package com.lynn.salary.web.action;

import java.io.File;
import java.util.Date;

import com.lynn.salary.entity.Accessory;
import com.lynn.salary.entity.SystemFile;
import com.lynn.salary.service.ISysArgumentService;
import com.lynn.salary.service.ISystemFileService;
import com.lynn.salary.util.ObjectFactory;
import com.lynn.salary.util.ToolMethod;

public class UploadFileAction extends BaseAction {

	private File uploadFile;
	private String uploadFileContentType;
	private String uploadFileFileName;

	private ISystemFileService systemFileService = (ISystemFileService) ObjectFactory.getBean("systemFileService");
	private ISysArgumentService sysArgumentService = (ISysArgumentService) ObjectFactory.getBean("sysArgumentService");
	private SystemFile systemFile;
	private Accessory accessory;

	public String forAccessory() throws Exception {
		System.out.println("uploadFile name:" + uploadFileFileName);
		System.out.println("uploadFile type:" + uploadFileContentType);
		uploadFileFileName = ToolMethod.replaceBlank(uploadFileFileName);

		String rootStorePath = sysArgumentService.getValue("rootStorePath");
		System.out.println("rootStorePath ==> " + rootStorePath);

		if (!rootStorePath.endsWith("/") && !rootStorePath.endsWith("\\")) {
			rootStorePath += File.separator;
		}
	 
		try {
			String abPath = pathStrategy();
			String path = rootStorePath + abPath;
			File nf = new File(path);
			if (!nf.exists())
				nf.mkdirs();
			path += uploadFileFileName;
			nf = new File(path);
			boolean flag = ToolMethod.copy(uploadFile, nf);
			if(!flag) {
				throw new Exception();
			}
			systemFile = createSystemFile();
			systemFile.setSavePath(abPath + uploadFileFileName);
			
			systemFile = systemFileService.save(systemFile);
			uploadFile.delete();
		} catch (Exception e) {
			e.printStackTrace();
			return "input";
		}
		
		return "uploadResponse";
	}

	/**
	 * 路径策略
	 * 
	 * @param path
	 * @return
	 */
	private String pathStrategy() {
		String d = ToolMethod.getDate("yyyyMM");
		return d + "/";
	}

	private SystemFile createSystemFile() {
		SystemFile systemFile = new SystemFile();
		Date d = new Date();
		systemFile.setAlterTime(d);
		systemFile.setNewTime(d);
		systemFile.setCreator(loginUser.getLoginUser().getId());
		systemFile.setFileName(uploadFileFileName);
		systemFile.setFileType(uploadFileContentType);
		systemFile.setFileSize(uploadFile.length() + "");
		systemFile.setTag(accessory.getId()+";");
		return systemFile;
	}

	public File getUploadFile() {
		return uploadFile;
	}

	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}

	public String getUploadFileContentType() {
		return uploadFileContentType;
	}

	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}

	public String getUploadFileFileName() {
		return uploadFileFileName;
	}

	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}

	public ISystemFileService getSystemFileService() {
		return systemFileService;
	}

	public void setSystemFileService(ISystemFileService systemFileService) {
		this.systemFileService = systemFileService;
	}

	public ISysArgumentService getSysArgumentService() {
		return sysArgumentService;
	}

	public Accessory getAccessory() {
		return accessory;
	}

	public void setAccessory(Accessory accessory) {
		this.accessory = accessory;
	}

	public void setSysArgumentService(ISysArgumentService sysArgumentService) {
		this.sysArgumentService = sysArgumentService;
	}

	public SystemFile getSystemFile() {
		return systemFile;
	}

	public void setSystemFile(SystemFile systemFile) {
		this.systemFile = systemFile;
	}

}
